import { CopyControllerBase } from "../view/copy/controller/CopyControllerBase";
import { HeroBattleIcon } from "../view/heroBattle/HeroBattleIcon";
import { IHeroBattleModule } from "../../../modules/interfaces/IHeroBattleModule";
import { HeroBattleModule } from "../../../modules/HeroBattleModule";
import { IHeroBattleResultModule } from "../../../modules/interfaces/IHeroBattleResultModule";
import { HeroBattleResultModule } from "../../../modules/HeroBattleResultModule";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { EPublicCommand } from "../../../Message/Command/EPublicCommand";
import { ECmdCell } from "../../../Message/Command/ECmdCell";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { RolePlayer } from "../scene/player/entity/RolePlayer";
import { PlayerEvent } from "../scene/events/PlayerEvent";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SChaosFightResultMsg } from "../../../Message/Public/SChaosFightResultMsg";
import { DataEvent } from "../events/DataEvent";
import { GameProxy } from "../mvc/GameProxy";
import { SSeqPlayerItem } from "../../../Message/Public/SSeqPlayerItem";
import { SChaosFightBattleInfo } from "../../../Message/Public/SChaosFightBattleInfo";
import { Alert } from "../../../com/mui/controls/Alert";
import { Language } from "../../../extend/language/Language";
import { CopyConfig } from "../resource/CopyConfig";
import { HeroBattleInfoPanel } from "../view/heroBattle/HeroBattleInfoPanel";
import { SkillComposePanel } from "../view/heroBattle/skillCompose/SkillComposePanel";
import { TCopy } from "../../../Message/Db/Tables/TCopy";
import { ThingUtil } from "../scene/layer/utils/ThingUtil";
type int = number;
//class HeroBattleController
    
    export  class HeroBattleController extends CopyControllerBase
    {
        constructor()
        {
            
            super();this._myArr = [];
            return;
        }

        /* internal  */get icon(): HeroBattleIcon
        {
            if (!this._icon) 
            {
                this._icon = new HeroBattleIcon();
                this._icon.updateIconInfo();
            }
            return this._icon;
        }

        /* internal  */get module(): IHeroBattleModule
        {
            if (!this._module) 
            {
                this._module = new HeroBattleModule();
            }
            return this._module;
        }

        /* internal  */get resultWin(): IHeroBattleResultModule
        {
            if (!this._resultWin) 
            {
                this._resultWin = new HeroBattleResultModule();
            }
            return this._resultWin;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicChaosFightOpen, this.activeOpen);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicChaosFightClose, this.activeClose);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicChaosFightBattleInfo, this.heroBattleInfo);
            NetDispatcher.addCmdListener(ECmdCell._ECmdCellPushChaosFightItemList, this.materialInfo);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicChaosFightResult, this.onResult);
            Dispatcher.addEventListener(EventName.OnClickHeroBattleIcon, this.onClickIcon);
            Dispatcher.addEventListener(EventName.OpenHeroBattleView, this.openView);
            Dispatcher.addEventListener(EventName.HeroBattleApply, this.onHeroBattleApply);
            Dispatcher.addEventListener(EventName.HeroBattleLeaveCopy, this.onLeaveHeroBattle);
            Dispatcher.addEventListener(EventName.HeroBattleComposeSkill, this.onComposeSkill);
            RolePlayer.instance.addEventListener(PlayerEvent.RoleLevelUpdate, this.onRoleLevelUpdateHandler);
            return;
        }

        /* internal  */onResult(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SChaosFightResultMsg;
            if (this.resultWin.isHide) 
            {
                this.resultWin.show();
            }
            this.resultWin.updateResult(loc1);
            return;
        }

        /* internal  */onComposeSkill(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            GameProxy.copy.composeSkill(loc1);
            return;
        }

        /* internal  */materialInfo(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SSeqPlayerItem;
            if (this._skillCompose) 
            {
                this._skillCompose.updateMaterial(loc1.playerItem);
            }
            return;
        }

        /* internal  */heroBattleInfo(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SChaosFightBattleInfo;
            cache.copy.heroBattleInfo = loc1;
            if (this._infoPanel) 
            {
                this._infoPanel.updateInfo(loc1);
            }
            return;
        }

        /* internal  */openView(arg1: DataEvent): void
        {
            var loc1=arg1.data.todayTimes;
            var loc2=arg1.data.maxTimes;
            this.module.updateTodayTimes(loc1, loc2);
            return;
        }

        /* internal  */onLeaveHeroBattle(arg1: DataEvent): void
        {
            Alert.show(Language.getString(200200), null, Alert.OK | Alert.CANCEL, null, this.closeLeaveCopyHandler);
            return;
        }

        /* internal  */closeLeaveCopyHandler(arg1: int): void
        {
            if (arg1 == Alert.OK) 
            {
                GameProxy.copy.leaveHeroBattle();
            }
            return;
        }

        /* internal  */onHeroBattleApply(arg1: DataEvent): void
        {
            GameProxy.copy.enterHeroBattle();
            return;
        }

        /* internal  */onClickIcon(arg1: DataEvent): void
        {
            var loc1=null;
            var loc2=null;
            if (this.module.isHide) 
            {
                this.module.show();
                loc1 = CopyConfig.instance.getInfoByCode(430);
                if (loc1 != null) 
                {
                    loc2 = loc1.reward.split(",");
                }
                this.module.updateBonuse(loc2);
                GameProxy.copy.clickHeroBattleIcon();
            }
            else 
            {
                this.module.hide();
            }
            return;
        }

        /* internal  */activeOpen(arg1: MessageBlock): void
        {
            if (cache.role.entityInfo.level >= 40) 
            {
                this.icon.show();
            }
            this.activeIsOpen = true;
            return;
        }

        /* internal  */activeClose(arg1: MessageBlock): void
        {
            this.icon.hide();
            this.activeIsOpen = false;
            return;
        }

        /* internal  */showHideInfoPanel(arg1: Boolean): void
        {
            if (arg1) 
            {
                if (this._infoPanel == null) 
                {
                    this._infoPanel = new HeroBattleInfoPanel();
                }
                this._infoPanel.show();
                this._infoPanel.updatePosition();
                if (cache.copy.heroBattleInfo) 
                {
                    this._infoPanel.updateInfo(cache.copy.heroBattleInfo);
                }
            }
            else if (this._infoPanel != null) 
            {
                this._infoPanel.hide();
                this._infoPanel.clear();
            }
            return;
        }

        /* internal  */showHideComposePanel(arg1: Boolean): void
        {
            if (arg1) 
            {
                if (this._skillCompose == null) 
                {
                    this._skillCompose = new SkillComposePanel();
                }
                this._skillCompose.show();
                this._skillCompose.updatePosition();
            }
            else if (this._skillCompose != null) 
            {
                this._skillCompose.hide();
                this._skillCompose.clearAllItem();
                this._skillCompose.clearMalterialItem();
            }
            return;
        }

        /* internal  */onRoleLevelUpdateHandler(arg1: Object): void
        {
            if (cache.role.entityInfo.level >= 40 && this.activeIsOpen) 
            {
                this.icon.show();
            }
            return;
        }

        public /* override */ enterCopy(arg1: TCopy): void
        {
            super.enterCopy(arg1);
            if (!this.module.isHide) 
            {
                this.module.hide();
            }
            this.showHideInfoPanel(true);
            this.showHideComposePanel(true);
            ThingUtil.entityUtil.showFabaoPlayer(false);
            return;
        }

        public /* override */ leaveCopy(): void
        {
            super.leaveCopy();
            this.showHideInfoPanel(false);
            this.showHideComposePanel(false);
            ThingUtil.entityUtil.showFabaoPlayer(true);
            return;
        }

        private /* var */_icon: HeroBattleIcon;

        private /* var */_module: IHeroBattleModule;

        private /* var */_infoPanel: HeroBattleInfoPanel;

        private /* var */_skillCompose: SkillComposePanel;

        private /* var */_resultWin: IHeroBattleResultModule;

        private /* var */_myArr: Array<any>;

        private /* var */activeIsOpen: Boolean=false;
    }
